package xupt.se.ttms.dao;

import java.net.PasswordAuthentication;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import xupt.se.ttms.idao.iPlayDAO;
import xupt.se.ttms.model.Order;
import xupt.se.ttms.model.Play;
import xupt.se.util.MyDBUtil;

public class PlayDAO implements iPlayDAO{
	private final String insertSql="insert into T_Play(mv_id,studio_id,play_time,play_price,ticket_count)values(?,?,?,?,?)";
	//通过主键id 删除
	private final String deleteSql="delete from T_Play where play_id=?";	
	
	private final String updateSql="update T_Play set mv_id=?,studio_id=?,play_time=?,play_price=?,ticket_count=? where play_id=?";
	//通过主键id查询
	private final String selectSqlInt="select * from T_Play where play_id=?";
	
	//查询全部
	private final String selectSqlAll="select * from T_Play ";
	//演出计划，根据mv_id 查询studio_id
	private final String selectSql_mv_id="select * from T_Play where mv_id=?";
	
	//根据电影id和演出厅id查询play信息
	private final String selectSqlMore="select * from T_Play where mv_id=? and studio_id=? and play_time=?";
	@Override
	public boolean execInsert(Play play) throws Exception {
		String sql=insertSql;
		Connection conn=null;
		boolean flag=false;
		PreparedStatement ps=null;
		MyDBUtil db = new MyDBUtil();
		conn=db.openConnection();
		try {
			if(null==conn)
				throw new Exception("Database not connected!");
			
			ps=conn.prepareStatement(sql);
			//ps.setInt(1,customer.);
			
			ps.setInt(1,play.getMv_id());
			ps.setInt(2,play.getStudio_id());
			ps.setString(3,play.getPlay_time());
			ps.setFloat(4,play.getPlay_price());
			ps.setInt(5, play.getTicket_count());
			flag=ps.execute();
			/*
			Statement stmt = conn.createStatement();
			flag = stmt.execute(sql);*/			
			ps.close();	
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return flag;
	}

	@Override
	public boolean execUpdate(Play play) throws Exception {
		String sql=updateSql;
		Connection conn=null;
		boolean flag=false;
		PreparedStatement ps=null;
		MyDBUtil db = new MyDBUtil();
		conn=db.openConnection();
		try {
			if(null==conn)
				throw new Exception("Database not connected!");
			
			ps=conn.prepareStatement(sql);
			ps.setInt(1,play.getMv_id());
			ps.setInt(2,play.getStudio_id());
			ps.setString(3,play.getPlay_time());
			ps.setFloat(4,play.getPlay_price());
			ps.setInt(5, play.getTicket_count());
			ps.setInt(6, play.getPlay_id());	
			flag=ps.execute();
			/*
			Statement stmt = conn.createStatement();
			flag = stmt.execute(sql);*/			
			ps.close();	
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return flag;
	}

	@Override
	public List<Play> HandlePlayResultSet(ResultSet rs) throws SQLException {
		 //结果，在第一个查询结果有个rs标记，通过rs.next()遍历获取全部
		Play play=null;
		List<Play> plays=new ArrayList<>();
		 //获取结果的数据
        ResultSetMetaData rsmd = rs.getMetaData();
        //获取该条结果有多少个列数据
        int columnCount = rsmd.getColumnCount();
        
       
    	System.out.println("获取演出计划数据中");
    	//通过rs.next()遍历获取全部结果
        while (rs.next()) {
        
        	play=new Play();
       	 //输出每条结果的全部数据
           for (int i = 0; i < columnCount; i++) {
        	 
        	   play.setPlay_id(rs.getInt("play_id"));
        	   play.setMv_id(rs.getInt("mv_id"));
        	   play.setStudio_id(rs.getInt("studio_id"));
        	   play.setPlay_time(rs.getString("play_time"));
        	   play.setPlay_price(rs.getFloat("play_price"));
        	   play.setTicket_count(rs.getInt("ticket_count"));
               System.out.print(rs.getString(i+1) + "\t");
           }
           plays.add(play);
           System.out.println();		                    
        }
    
        return plays;
     
	}

	@Override
	public ResultSet execQuery(int i) throws Exception {
		String sql=selectSqlInt;
		ResultSet rstSet = null;
		MyDBUtil myDBUtil=new MyDBUtil();
		rstSet=myDBUtil.execQuery(sql, i);
		return rstSet;
	}

	@Override
	public ResultSet exexQueryAll() throws Exception {
		String sql=selectSqlAll;
		ResultSet rstSet = null;
		MyDBUtil myDBUtil=new MyDBUtil();
		rstSet=myDBUtil.execAllQuery(sql);
		return rstSet;
	}

	@Override
	public boolean execDelete(int i) throws Exception {
		String sql=deleteSql;
		boolean flag = false;
		MyDBUtil myDBUtil=new MyDBUtil();
		flag=myDBUtil.execDelete(sql, i);
		return flag;
	}

	@Override
	public ResultSet execMv_id(int i) throws Exception {
		String sql=selectSql_mv_id;
		ResultSet rstSet = null;
		MyDBUtil myDBUtil=new MyDBUtil();
		rstSet=myDBUtil.execQuery(sql, i);
		return rstSet;
	}

	@Override
	public ResultSet execPlay(int mv_id, int studio_id, String time) throws Exception {
		String sql=selectSqlMore;
		ResultSet rstSet = null;
		MyDBUtil myDBUtil=new MyDBUtil();
		rstSet=myDBUtil.execQuery(sql, mv_id, studio_id, time);
		return rstSet;
	}



	


}
